﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cinser.Common;
using System.Xml;

namespace Cinser.DataTranslate.SettingManager
{
    public class TaskSetting:SettingBase
    {
        string logId, settingId;
        DateTime beginDate, finishDate;
        int totalTaskCount, errorNum;
        double finishPercent;
        TaskManager.TaskBase task = null;
        public enum TaskType
        {
            Import,
            Export
        }
        TaskType type;

        public TaskType Type
        {
            get { return type; }
            set { type = value; }
        }

        public TaskManager.TaskBase Task
        {
            get
            {
                if (task == null)
                {
                    if (Type == TaskType.Import)
                    {
                        ImportSettings s = SettingsManager.GetImportSettings().Where(b => b.Id == this.SettingId).ToList()[0];
                        task = new TaskManager.ImportTask(s);
                        task.BeginDate = this.BeginDate;
                        task.FinishDate = this.FinishDate;
                        task.ErrorNum = this.ErrorNum;
                        task.TotalTaskFinishPercent =  this.FinishPercent;
                        task.Id = this.Id;
                        task.TotalTaskCount = this.TotalTaskCount;
                        task.TaskName = SettingName;
                        
                    }
                }
                return task;
            }
        }

        public TaskSetting()
        {
        }
        public TaskSetting(string id, string taskName,string logId, string settingId, DateTime beginDate, DateTime finishDate, int totalTaskCount, int errorNum, double finishPercent)
        {
            this.logId = logId;
            this.settingId = settingId;
            this.totalTaskCount = totalTaskCount;
            this.beginDate = beginDate;
            this.errorNum = errorNum;
            this.finishDate = finishDate;
            this.finishPercent = finishPercent;
            this.Id = id;
            this.SettingName = taskName;
        }

        public string SettingId
        {
            get { return settingId; }
            set { settingId = value; }
        }

        public string LogId
        {
            get { return logId; }
            set { logId = value; }
        }

        public DateTime FinishDate
        {
            get { return finishDate; }
            set { finishDate = value; }
        }

        public DateTime BeginDate
        {
            get { return beginDate; }
            set { beginDate = value; }
        }

        public int ErrorNum
        {
            get { return errorNum; }
            set { errorNum = value; }
        }

        public int TotalTaskCount
        {
            get { return totalTaskCount; }
            set { totalTaskCount = value; }
        }

        public double FinishPercent
        {
            get { return finishPercent; }
            set { finishPercent = value; }
        }

        public override void SaveSettings()
        {
            string xmlPath = SettingsManager.GetTaskSavePath();
            Cinser.Common.XmlHelper xml = new Cinser.Common.XmlHelper(xmlPath);
            if (xml.FirstNode.FindNode("Item", "Id", this.Id, false) != null)
            {
                xml.FirstNode.UpdateNode("Item", new List<string>() { "Id" }, new List<string>() { this.Id }, false, new XmlUtils.XmlNodeHandler((node) =>
                {
                    //update
                    node.SetNodeAttribute("Id", this.Id);
                    node.SetNodeAttribute("TaskName", this.SettingName);
                    node.SetNodeAttribute("LogId", this.LogId);
                    node.SetNodeAttribute("SettingId", this.SettingId);
                    node.SetNodeAttribute("BeginDate", this.BeginDate.ToString());
                    node.SetNodeAttribute("FinishDate", this.FinishDate.ToString());
                    node.SetNodeAttribute("TotalTaskCount", this.TotalTaskCount.ToString());
                    node.SetNodeAttribute("ErrorNum", this.ErrorNum.ToString());
                    node.SetNodeAttribute("FinishPercent", this.FinishPercent.ToString());
                }));
                xml.XmlDocument.Save(xmlPath);
            }
            else
            {
                //add
                XmlNode node = xml.XmlDocument.CreateElement("Item") as XmlNode;
                node.SetNodeAttribute("Id", this.Id);
                node.SetNodeAttribute("TaskName", this.SettingName);
                node.SetNodeAttribute("LogId", this.LogId);
                node.SetNodeAttribute("SettingId", this.SettingId);
                node.SetNodeAttribute("BeginDate", this.BeginDate.ToString());
                node.SetNodeAttribute("FinishDate", this.FinishDate.ToString());
                node.SetNodeAttribute("TotalTaskCount", this.TotalTaskCount.ToString());
                node.SetNodeAttribute("ErrorNum", this.ErrorNum.ToString());
                node.SetNodeAttribute("FinishPercent", this.FinishPercent.ToString());

                xml.FirstNode.AppendChild(node);
                xml.XmlDocument.Save(xmlPath);
            }
        }

        public override string GetSettingPath()
        {
            return SettingsManager.GetTaskSavePath();
        }

        public override bool Delete()
        {
            bool bReturn = false;
            bReturn = base.Delete();
            if (bReturn == true)
            {
                //删除任务日志文件
                List<string> files = null;
                //files = Cinser.Common.FileHelper.GetFileNamesFromADirectory(AppUtility.LogFolderPath, "txt");
                if (files != null && files.Count > 0)
                {
                    foreach (var item in files)
                    {
                        if (item.IndexOf(this.Task.Id) != -1)
                        {
                            System.IO.File.Delete(item);
                        }
                    }
                }
            }
            return bReturn;
        }
    }
}
